// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';
/**
 * qs是一个npm仓库所管理的包，一个增加了一些安全性的查询字符串解析和序列化字符串的库。
 * qs.parse()将URL解析成对象的形式
 * qs.stringify()将对象序列化成URL的形式，以&进行拼接(可用于发送查询条件)
 */
import qs from 'qs';

/** 获取当前的用户 GET /api/currentUser */
export async function currentUser(
  param?: any,
  options?: {
    [key: string]: any;
  },
): Promise<COMMON.BaseSuccess<HOMEAPI.CurrentUser>> {
  return request('/api/portal/user/getCurrentUser', {
    method: 'GET',
    param,
    ...(options || {}),
  });
}

/** 退出登录接口 POST /api/login/outLogin */
export async function outLogin(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/logout', {
    method: 'DELETE',
    ...(options || {}),
  });
}

/** 登录接口 POST /api/login/account */
export async function login(
  body: HOMEAPI.LoginForm,
  options?: { [key: string]: any },
): Promise<
  COMMON.BaseSuccess<
    COMMON.TokenType & {
      tokenSurvivalTime?: number;
    }
  >
> {
  return request('/api/authorization/token', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(body),
    ...(options || {}),
  });
}
/**
 *  手机验证码登录
 * @param body  手机号 & 验证码
 * @returns
 */
export async function phoneLoginS(body: HOMEAPI.PhoneLoginT): Promise<
  COMMON.BaseSuccess<
    COMMON.TokenType & {
      tokenSurvivalTime?: number;
    }
  >
> {
  return request('/api/authorization/token/mobile', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: qs.stringify(body),
  });
}
/**
 * 获取手机验证码
 * @param mobile 手机号
 * @returns
 */
export async function sendVerifyCode(mobile: string): Promise<COMMON.BaseSuccess> {
  return request('/api/authorization/sms/sendVerifyCode', {
    method: 'POST',
    data: qs.stringify({ mobile }),
  });
}
/** 注册接口 */
export async function register(body: HOMEAPI.LoginForm): Promise<COMMON.BaseSuccess<string>> {
  return request('/api/portal/user/register', {
    method: 'POST',
    // headers: {
    //   'Content-Type': 'application/x-www-form-urlencoded',
    // },
    // data: qs.stringify(body),
    data: body,
  });
}

export async function getNotices(options?: { [key: string]: any }) {
  return request<HOMEAPI.NoticeIconList>('/api/notices', {
    method: 'GET',
    ...(options || {}),
  });
}

// 获取当前ip
export async function getIp(options?: { [key: string]: any }) {
  return request<HOMEAPI.NoticeIconList>('/api/findIP', {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 获取用户列表
 */
export async function rule(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
  },
  options?: { [key: string]: any },
) {
  return request<HOMEAPI.RuleList>('/api/user/list', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 新建规则 PUT /api/rule */
export async function updateRule(options?: { [key: string]: any }) {
  return request<HOMEAPI.RuleListItem>('/api/rule', {
    method: 'PUT',
    ...(options || {}),
  });
}

/** 新建规则 POST /api/rule */
export async function addRule(options?: { [key: string]: any }) {
  return request<HOMEAPI.RuleListItem>('/api/rule', {
    method: 'POST',
    ...(options || {}),
  });
}

/** 删除规则 DELETE /api/rule */
export async function removeRule(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/rule', {
    method: 'DELETE',
    ...(options || {}),
  });
}
